package com.tough.engine.scene;

import javax.microedition.lcdui.Graphics;

import com.tough.engine.GmConst;
import com.tough.engine.base.AABB;
import com.tough.engine.base.Body;
import com.tough.engine.base.BodyDef;
import com.tough.engine.base.BodyType;
import com.tough.engine.base.FixtureDef;
import com.tough.engine.base.World;

public class StageTestPlace implements Stage {

	private final byte RATE = 100;
	private final int PUSH_ACTOR_FORCE = 50 * RATE;
	private final int BOUNCE_ACTOR_FORCE = 280 * RATE;
	private World world;
	private Body actor;
	
	private int[][] ground_positions = {
			{0,100},
			{100,110},
			{250,90},
			{280,250},
			{440,305},
			{330,250},
	};
	private int[][] grounds_sizes = {
			{90,20},
			{100,20},
			{30,10},
			{70,15},
			{70,15},
			{30,40},
	};
	
	private Body[] grounds = new Body[grounds_sizes.length];
	
	private int[] aabbStartPosition = {0, 0};
	private int[] aabbRangeSize = {GmConst.WIDTH, GmConst.HEIGHT};
	
	public StageTestPlace(){
		world = new World(10*RATE);
		world.setAABB(createAABB(aabbStartPosition, aabbRangeSize));
		
		createActor();
		for (int i = 0; i < grounds.length; i++) {
			grounds[i] = createGround(ground_positions[i],grounds_sizes[i]);
		}
	}

	public AABB createAABB(int[] startPosition, int[] rangeSize){
		AABB 
		aabb = new AABB();
		aabb.setStartPosition(startPosition[0], startPosition[1]);
		aabb.setRangeSize(rangeSize[0], rangeSize[1]);
		
		return aabb;
	}
	
	private void createActor() {
		BodyDef 
		_bodyDef = new BodyDef();
		_bodyDef.type = BodyType.DYNAMIC;
		_bodyDef.position.set(80, 0);
		actor = world.createBody(_bodyDef);
		
		FixtureDef 
		_fixtureDef = new FixtureDef();
		_fixtureDef.size.set(50, 50);
		_fixtureDef.mass = 20;
		actor.createFixtureDef(_fixtureDef);
	}
	
	private Body createGround(int[] position, int[] size) {
		Body ground = null;
		BodyDef 
		_bodyDef = new BodyDef();
		_bodyDef.position.set(position[0], position[1]);
		ground = world.createBody(_bodyDef);
		
		FixtureDef 
		_fixtureDef = new FixtureDef();
		_fixtureDef.size.set((int)size[0], (int)size[1]);
		_fixtureDef.mass = 50 * RATE;
		_fixtureDef.friction = 30 * RATE;
		ground.createFixtureDef(_fixtureDef);
		
		return ground;
	}
	
	public void clean(Graphics g) {

	}

	public void draw(Graphics g) {
		g.setColor(GmConst.COLOR_BLACK);
		g.fillRect(0, 0, GmConst.WIDTH, GmConst.HEIGHT);
		g.setColor(GmConst.COLOR_WHITE);
		g.fillRect((int)actor.getPosition().x, (int)actor.getPosition().y, (int)actor.getSize().w, (int)actor.getSize().h);
		g.setColor(GmConst.COLOR_BLUE_SKY);
		for (int i = 0; i < grounds.length; i++) {
			g.fillRect((int)grounds[i].getPosition().x, (int)grounds[i].getPosition().y, (int)grounds[i].getSize().w, (int)grounds[i].getSize().h);
		}
	}

	public void keyPressed(int keyCode) {
		if(keyCode == GmConst.KEY_RIGHT){
			world.push(actor, PUSH_ACTOR_FORCE);
		}else if(keyCode == GmConst.KEY_LEFT){
			world.push(actor, -PUSH_ACTOR_FORCE);
		}else if(keyCode == GmConst.KEY_UP){
			world.bounce(actor, BOUNCE_ACTOR_FORCE);
		}
	}

	public void keyReleased(int keyCode) {
		if(keyCode == GmConst.KEY_RIGHT || keyCode == GmConst.KEY_LEFT){
			world.release(actor);
//			System.out.println("~~~~~~~~~~~~~");
		}else if(keyCode == GmConst.KEY_UP){
			world.bounce(actor, 0);
		}
	}

	public void logic() {
		world.calculate(GmConst.currentTime);
	}

	public void reset() {

	}

}
